﻿namespace Little.Soldier.SWIFT.Validation
{
  public class ValidationRule_Range : ValidationRuleBase
  {
    private double U0001;
    private double U0002;

    public ValidationRule_Range(double min, double max)
    {
      this.U0001 = min;
      this.U0002 = max;
    }

    public ValidationRule_Range(double min, double max, char delimiter, int count)
    {
      this.U0001 = min;
      this.U0002 = max;
      this.Delimiter = delimiter;
      this.Count = count;
    }

    public ValidationRule_Range(double min, double max, int startIndex)
    {
      this.U0001 = min;
      this.U0002 = max;
      this.StartIndex = startIndex;
    }

    public ValidationRule_Range(double min, double max, int startIndex, int length)
    {
      this.U0001 = min;
      this.U0002 = max;
      this.StartIndex = startIndex;
      this.Length = length;
    }

    public override ValidationError[] Validate(string value)
    {
      double result = double.MinValue;
      if (!double.TryParse(value, out result))
      {
        if (string.IsNullOrEmpty(this.ErrorDescription))
          return new ValidationError[1]
          {
            new ValidationError(ValidationErrorType.WrongRange, string.Format("The value '{0}' must be type Double.", (object) value))
          };
        else
          return new ValidationError[1]
          {
            new ValidationError(ValidationErrorType.WrongRange, this.ErrorDescription)
          };
      }
      else
      {
        if (result >= this.U0001 && result <= this.U0002)
          return new ValidationError[0];
        if (string.IsNullOrEmpty(this.ErrorDescription))
          return new ValidationError[1]
          {
            new ValidationError(ValidationErrorType.WrongRange, string.Format("The range of the value '{2}' must be between {0} and {1}.", (object) this.U0001, (object) this.U0002, (object) value))
          };
        else
          return new ValidationError[1]
          {
            new ValidationError(ValidationErrorType.WrongRange, this.ErrorDescription)
          };
      }
    }
  }
}
